<template>
  <button
    class="md-button"
    :class="{
      'md-button--primary': type === 'primary',
      'md-button--text': type === 'text',
      'md-button--active': active,
      'md-button--disabled': disabled
    }"
    :disabled="disabled"
    @click="handleClick"
  >
    <span v-if="icon" class="md-button__icon">
      <slot name="icon">
        <span v-if="icon.startsWith('<svg')" v-html="icon"></span>
        <span v-else>{{ icon }}</span>
      </slot>
    </span>
    <span v-if="$slots.default" class="md-button__text">
      <slot></slot>
    </span>
  </button>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue';

interface Props {
  type?: 'default' | 'primary' | 'text';
  icon?: string;
  active?: boolean;
  disabled?: boolean;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (e: 'click', event: MouseEvent): void;
}>();

const handleClick = (event: MouseEvent) => {
  if (!props.disabled) {
    emit('click', event);
  }
};
</script>

<style lang="scss" scoped>
.md-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 4px;
  border: 1px solid var(--md-borderColor, #e0e0e0);
  background-color: var(--md-backgroundColor, #ffffff);
  color: var(--md-textColor, #333333);
  cursor: pointer;
  transition: all 0.3s;
  outline: none;
  
  &:hover {
    background-color: var(--md-hoverColor, #f5f5f5);
  }
  
  &--primary {
    background-color: var(--md-activeColor, #1890ff);
    border-color: var(--md-activeColor, #1890ff);
    color: #ffffff;
    
    &:hover {
      background-color: var(--md-hoverColor, #40a9ff);
      border-color: var(--md-hoverColor, #40a9ff);
    }
  }
  
  &--text {
    border: none;
    background: transparent;
    padding: 4px 8px;
    
    &:hover {
      background-color: rgba(0, 0, 0, 0.05);
    }
  }
  
  &--active {
    background-color: var(--md-activeColor, #1890ff);
    border-color: var(--md-activeColor, #1890ff);
    color: #ffffff;
  }
  
  &--disabled {
    cursor: not-allowed;
    opacity: 0.6;
    
    &:hover {
      background-color: var(--md-backgroundColor, #ffffff);
      border-color: var(--md-borderColor, #e0e0e0);
    }
    
    &.md-button--primary:hover {
      background-color: var(--md-activeColor, #1890ff);
      border-color: var(--md-activeColor, #1890ff);
    }
  }
  
  &__icon {
    margin-right: 4px;
    display: inline-flex;
    align-items: center;
  }
}
</style>